package com.ynny.lml.dao;

import com.ynny.lml.bean.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public abstract class StudentDaoImpl implements StudentDao {

    private final JdbcTemplate jdbcTemplate;

    // 使用构造器注入替代字段注入
    public StudentDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int insert() {
        String sql = "insert into student(number, name, grade, clbum, gender) values (?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, "2023001", "张三", "2023", "语文", "男");
    }

    @Override
    public int update(String number, String newName) {
        String sql = "update student set name = ? where number = ?";
        return jdbcTemplate.update(sql, newName, number);
    }

    @Override
    public Student selectOne(String number) {
        String sql = "select * from student where number = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Student.class), number);
    }

    @Override
    public int delete(String number) {
        String sql = "delete from student where number = ?";
        return jdbcTemplate.update(sql, number);
    }

    @Override
    public List<Student> selectAll() {
        String sql = "select * from student";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
    }
}
